package book.controller;

import book.entity.*;
import book.service.*;
import book.util.ExcelUtil;

import book.entity.leixing;
import book.entity.tushu;
import book.service.leixingServices;
import book.util.ExcelUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import book.service.TushuServices;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/tushu")
public class tushuController {

    @Autowired
    private TushuServices tushuServices;

    @Autowired
    private leixingServices leixingServices;

    @Autowired
    private chubansheServices chubansheServices;

    @Autowired
    private JieYueService jieYueService;
    @Autowired
    private DuzheServices duzheServices;
    @Autowired
    private FaJinService fajinService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request){
        List<leixing> leixings = leixingServices.getAll();
        request.setAttribute("leixings",leixings);
        return "book/list";
    }

    @RequestMapping(value = "/list",
            method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> list(@RequestParam("offset") int offset,@RequestParam("limit") int limit,tushu condition){
        List<tushu> tushus = tushuServices.getByPage(offset,limit,condition);
        int total = tushuServices.getCountByCondition(condition);
        for(tushu rt : tushus){
            System.out.println(rt.getChubanshe().getCbs_name());
            System.out.println(rt.getBook_img());
            System.out.println(rt.getTu_name());
            System.out.println(rt.getLx().getLx_name());

        }
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("total",total);
        map.put("rows",tushus);
        return map;
    }


    //打开图书界面
    @RequestMapping(value = "/list1")
    public String list1(HttpServletRequest request){
        List<leixing> leixings = leixingServices.getAll();

        request.setAttribute("leixings",leixings);

        return "book/list";
    }



    //打开新增图书界面
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(HttpServletRequest request){
        List<leixing> leixingList = leixingServices.getAll();
        List<chubanshe> chubansheList = chubansheServices.getAll();
        System.out.println("**************8"+ JSON.toJSONString(leixingList));
        request.setAttribute("leixing",leixingList);
        request.setAttribute("chubanshe",chubansheList);
        return "book/add";
    }
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> doAdd(HttpServletRequest request,@RequestParam("book") MultipartFile multipartFile,tushu tushu){
        //返回相对路径的真实路径  application.getRealPath("/upload");
        //获取原始上传文件名
        String oldFileName = multipartFile.getOriginalFilename();
        //获取系统时间差(现在-1970-1-1)毫秒
        //String newFileName = oldFileName+"_"+System.currentTimeMillis();
        //后缀
        String suffix = oldFileName.substring(oldFileName.lastIndexOf("."));
        String newFileName = oldFileName.substring(0,oldFileName.lastIndexOf("."))+"_"+System.currentTimeMillis()+suffix;
        String url = request.getServletContext().getRealPath("/upload");
        File targetFile = new File(url,newFileName);
        try {
            multipartFile.transferTo(targetFile);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        tushu.setBook_img("upload/"+newFileName);
        tushuServices.insertEntity(tushu);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("result",true);
        return map;
    }


    //修改
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public String toEdit(@RequestParam("ts_id") int id,Map<String,Object> map){
        tushu tushu = tushuServices.getById(id);
        List<leixing> tushuList = leixingServices.getAll();
        List<chubanshe> chubansheList = chubansheServices.getAll();
        map.put("leixing",tushuList);
        map.put("chubanshe",chubansheList);
        map.put("tushu",tushu);
        return "book/update";
    }

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> doEdit(HttpServletRequest request,@RequestParam("book") MultipartFile multipartFile,tushu tushu){
        //返回相对路径的真实路径  application.getRealPath("/upload");
        //获取原始上传文件名
        String oldFileName = multipartFile.getOriginalFilename();
        Map<String,Object> map = new HashMap<String,Object>();
        if(oldFileName!=null && oldFileName!="") {
            //获取系统时间差(现在-1970-1-1)毫秒
            //String newFileName = oldFileName+"_"+System.currentTimeMillis();
            //后缀
            String suffix = oldFileName.substring(oldFileName.lastIndexOf("."));
            String newFileName = oldFileName.substring(0,oldFileName.lastIndexOf("."))+"_"+System.currentTimeMillis()+suffix;
            String url = request.getServletContext().getRealPath("/upload");
            File targetFile = new File(url,newFileName);
            try {
                multipartFile.transferTo(targetFile);
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            tushu.setBook_img("upload/"+newFileName);
            System.out.println("dfsfsd "+JSON.toJSONString(tushu));
            tushuServices.update(tushu);

            map.put("result",true);
        }else{
            tushuServices.update(tushu);
            map.put("result",true);
        }
        return map;
    }







    @RequestMapping(value="/delete",
            method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> delete(@RequestParam("ts_id")int id){
        Map<String, Object> map = new HashMap<String,Object>();
        try {
            tushuServices.delete(id);
            map.put("result",true);
        }catch (Exception e){
            map.put("result",false);
        }
        return map;
    }
    //批量删除
    @RequestMapping(value="/batchDel",
            method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> batchDel(IdsModel ids){

        //3. 获取js传递过来的编号集合
        tushuServices.batchDel(ids.getIds());
        //4. service batchDel
        Map<String, Object> map = new HashMap<String,Object>();
        map.put("result",true);
        return map;
    }

    @RequestMapping("/batchAdd")
    public String batcAdd(HttpServletRequest request){
        return "book/batchAdd";
    }

    @RequestMapping(value = "/batchAdd",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> batchAdd(MultipartHttpServletRequest multipartHttpServletRequest) throws Exception{

        Map<String, Object> map = new HashMap<String,Object>();
        MultipartFile multipartFile = multipartHttpServletRequest.getFile("myFile");
        List<List<Object>> list = ExcelUtil.getExcelList(multipartFile.getInputStream(),multipartFile.getOriginalFilename());
        for(int i = 0;i<list.size();i++){
            List<Object> li = list.get(i);
            tushu t=new tushu();
            leixing l=new leixing();
            chubanshe c=new chubanshe();
            c.setCbs_id(Integer.parseInt(li.get(6).toString()));
            l.setLx_id(Integer.parseInt(li.get(5).toString()));
            t.setTu_name(li.get(0).toString());
            t.setJieyue(Integer.parseInt(li.get(1).toString()));
            t.setChushou(Integer.parseInt(li.get(2).toString()));
            t.setPosun(Integer.parseInt(li.get(3).toString()));
            t.setCishu(Integer.parseInt(li.get(4).toString()));
            t.setChubanshe(c);
            t.setLx(l);
            t.setZt_id(Integer.parseInt(li.get(7).toString()));
            t.setBook_img("static/img/a1.jpg");
            tushuServices.insertEntity(t);
        }
        map.put("result",true);
        return map;

    }



    //借阅
    @RequestMapping(value = "/jieyue",method = RequestMethod.GET)
    public String jieyue(HttpServletRequest request){
        List<leixing> leixings = leixingServices.getAll();
        request.setAttribute("leixings",leixings);

        return "jieyue/list1";
    }


    @RequestMapping(value = "/jieyue",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> jieyue(@RequestParam("offset") int offset,@RequestParam("limit") int limit,tushu condition){
        List<tushu> tushus = tushuServices.getByPage2(offset,limit,condition);
        int total = tushuServices.getCount2(condition);
        for(tushu rt : tushus){
            System.out.println(rt.getChubanshe().getCbs_name());
            System.out.println(rt.getBook_img());
            System.out.println(rt.getTu_name());
            System.out.println(rt.getLx().getLx_name());
        }
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("total",total);
        map.put("rows",tushus);
        return map;
    }

    //借阅
    @RequestMapping(value = "/borrow",method = RequestMethod.GET)
    public String borrow(@RequestParam("ts_id") String ts_id,jilu jieyue,HttpServletRequest request){

        tushu tushu = tushuServices.getById( Integer.parseInt(ts_id));
//        jieyue.setTs_id(tushu.getTs_id());
//        tushuServices.updateborrow(Integer.parseInt(ts_id));
//        jieYueService.insetjieyue(jieyue);

        System.out.println("sdsdsafdsa"+JSON.toJSONString(tushu));
        request.setAttribute("tushus",tushu);

        return "jieyue/jieyue";
    }

    //dakai
    @RequestMapping(value = "/jieyuexinxi",method = RequestMethod.GET)
    public String jieyuexinxi(HttpServletRequest request){
        List<leixing> leixings = leixingServices.getAll();
        request.setAttribute("leixings",leixings);
        return "jieyuexinxi/list2";
    }


    //借阅信息
    @RequestMapping(value = "/jieyuexinxi2",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> jieyuexinxi(HttpServletRequest request,@RequestParam("offset") int offset,@RequestParam("limit") int limit,jilu condition){
        List<leixing> leixings = leixingServices.getAll();
        request.setAttribute("leixings",leixings);
        List<jilu> jilus = jieYueService.getByPage(offset,limit,condition);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("rows",jilus);
        return map;
    }


    @RequestMapping(value = "/jieyuexinxi1",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> doAddjieyue( jilu jilu){
       jieYueService.insetEntity(jilu);
        tushuServices.updatezt(jilu.getTs_id());
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("result",true);
        return map;
    }
//    @RequestMapping("/batchAdd")
//    public String batcAdd(HttpServletRequest request){
//        return "book/batchAdd";
//    }
//
//    @RequestMapping(value = "/batchAdd",method = RequestMethod.POST)
//    @ResponseBody
//    public Map<String,Object> batchAdd(MultipartHttpServletRequest multipartHttpServletRequest) throws Exception{
//
//        Map<String, Object> map = new HashMap<String,Object>();
//        MultipartFile multipartFile = multipartHttpServletRequest.getFile("myFile");
//        List<List<Object>> list = ExcelUtil.getExcelList(multipartFile.getInputStream(),multipartFile.getOriginalFilename());
//        for(int i = 0;i<list.size();i++){
//            List<Object> li = list.get(i);
//            tushu t=new tushu();
//            leixing l=new leixing();
//            chubanshe c=new chubanshe();
//            c.setCbs_id(Integer.parseInt(li.get(6).toString()));
//            l.setLx_id(Integer.parseInt(li.get(5).toString()));
//            t.setTu_name(li.get(0).toString());
//            t.setJieyue(Integer.parseInt(li.get(1).toString()));
//            t.setChushou(Integer.parseInt(li.get(2).toString()));
//            t.setPosun(Integer.parseInt(li.get(3).toString()));
//            t.setCishu(Integer.parseInt(li.get(4).toString()));
//            t.setChubanshe(c);
//            t.setLx(l);
//            t.setZt_id(Integer.parseInt(li.get(7).toString()));
//            t.setBook_img("static/img/a1.jpg");
//            tushuServices.insertEntity(t);
//        }
//        map.put("result",true);
//        return map;
//
//    }

    //统计
    @RequestMapping(value = "/tongji",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> tongji(){

        List<tushu> tushus = tushuServices.tongji();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("tushus",tushus);
        return map;
    }
}
